<?php

namespace app\common\command;

use app\common\enums\AppointmentStatusEnum;
use app\common\model\banxi\core\CoreCourseAppointment;
use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * 配置
 */
class AutoExpired extends Command
{
    protected function configure()
    {
        $this->setName('AutoExpired')
            // ->addArgument('name', Argument::OPTIONAL, "your name")
            // ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('the class schedule has automatically expired');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln("排课自动过期定时任务开始");
        $this->doCommand();
        $output->writeln("排课自动过期定时任务结束");
    }

    protected function doCommand()
    {
        $dateTime = date('Y-m-d H:i:s');
        $yesterday = date('Y-m-d H:i:s', strtotime('-1 day'));
        $coreCourseAppointmentModel = new CoreCourseAppointment();

        $coreCourseAppointmentModel->whereRaw(
            "((CONCAT(`date`,' ',`endTime`) <= :dateTime AND endTime != '00:00:00') OR (CONCAT(`date`,' ',`endTime`) <= :yesterday AND endTime = '00:00:00'))",
            ['dateTime' => $dateTime, 'yesterday' => $yesterday]
        )->where('appointmentStatus', AppointmentStatusEnum::PENDING_CLASS)
            ->update(['appointmentStatus' => AppointmentStatusEnum::EXPIRED]);
        return true;
    }

}